package com.flyemu.share.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Dict;
import com.alibaba.fastjson.JSONArray;
import com.flyemu.share.api.Fenxi;
import com.flyemu.share.entity.*;
import com.flyemu.share.exception.ServiceException;
import com.flyemu.share.repository.RelationSubjectRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class RelationSubjectService extends AbsService {
    private final Fenxi fenxi;
    private final RelationSubjectRepository subjectRepository;
    private static final QRelationSubject qRelationSubject = QRelationSubject.relationSubject;
    private final RelationAccountService relationAccountService;

    @Transactional
    public JSONArray loadSubject(Long accountSetsId,Long merchantId,  Long organizationId) {
        List<Dict> dicts = new ArrayList<>();
        try {
            RelationAccount relationAccount = relationAccountService.load(merchantId,organizationId);
            JSONArray array = fenxi.loadSubject(relationAccount,accountSetsId);
//        for (int i = 0; i < array.size(); i++) {
//            JSONObject object = array.getJSONObject(i);
//            Dict dict = new Dict().set("subjectId", object.getString("id"))
//                    .set("subjectName", object.getString("fullName"))
//                    .set("subjectCode", object.getString("code"))
//                    .set("name", object.getString("name"));
//            dicts.add(dict);
//        }
            return array;
        } catch (Exception e) {
            throw new ServiceException("连接财务系统失败");
        }
    }


    public JSONArray loadAccountingCategory(Long accountSetsId, Set<Long> categoryIdSet,Long merchantId,  Long organizationId) {
        RelationAccount relationAccount = relationAccountService.load(merchantId,organizationId);

        JSONArray array = fenxi.loadAccountingCategory(relationAccount, accountSetsId, categoryIdSet).getJSONArray("data");
        return array;
    }

    public List<RelationSubject> query(Long cwRelationId, Long merchantId, Long organizationId) {
        List<RelationSubject> subjects = new ArrayList<>();
        bqf.selectFrom(qRelationSubject)
                .where(qRelationSubject.cwRelationId.eq(cwRelationId).and(qRelationSubject.merchantId.eq(merchantId).and(qRelationSubject.organizationId.eq(organizationId))))
                .fetch().forEach(tuple -> {
                    RelationSubject relationSubject = BeanUtil.toBean(tuple, RelationSubject.class);
                    subjects.add(relationSubject);
                });
        return subjects;
    }

    /**
     * 更新
     *
     * @param relationSubjects
     * @return
     */
    @Transactional
    public void save(List<RelationSubject> relationSubjects, Long merchantId, Long organizationId) {
        subjectRepository.saveAll(relationSubjects);
    }

}
